﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace eProject.ViewModel.Commands
{
    public class ChangeSettingsCommand : ICommand
    {
        private readonly CommonManager _manager;
        private readonly IList<double> _newValues;
        private readonly IList<double> _oldValues;


        private bool _isCanceled;
        private bool _isExecuted;

        public ChangeSettingsCommand(CommonManager manager, IEnumerable<double> manhours)
        {
            if (manager == null) throw new ArgumentNullException("manager");
            if (manhours == null) throw new ArgumentNullException("manhours");
            if (manhours.Count() != 7) throw new ArgumentException("length of manhours must be 7");
            _manager = manager;
            _newValues = new List<double>(manhours);
            _oldValues = new List<double> {manager.GetBusinessDayLength(DayOfWeek.Monday),manager.GetBusinessDayLength(DayOfWeek.Tuesday),
            manager.GetBusinessDayLength(DayOfWeek.Wednesday),manager.GetBusinessDayLength(DayOfWeek.Thursday),manager.GetBusinessDayLength(DayOfWeek.Friday),
            manager.GetBusinessDayLength(DayOfWeek.Saturday),manager.GetBusinessDayLength(DayOfWeek.Sunday),};

        }

        public void Execute()
        {
            if (_isExecuted) return;
            _manager.SetBusinessDayLength(DayOfWeek.Monday, _newValues[0]);
            _manager.SetBusinessDayLength(DayOfWeek.Tuesday, _newValues[1]);
            _manager.SetBusinessDayLength(DayOfWeek.Wednesday, _newValues[2]);
            _manager.SetBusinessDayLength(DayOfWeek.Thursday, _newValues[3]);
            _manager.SetBusinessDayLength(DayOfWeek.Friday, _newValues[4]);
            _manager.SetBusinessDayLength(DayOfWeek.Saturday, _newValues[5]);
            _manager.SetBusinessDayLength(DayOfWeek.Sunday, _newValues[6]);
            _isExecuted = true;
            _isCanceled = false;
        }

        public void Unexecute()
        {
            if (_isCanceled) return;
            _manager.SetBusinessDayLength(DayOfWeek.Monday, _oldValues[0]);
            _manager.SetBusinessDayLength(DayOfWeek.Tuesday, _oldValues[1]);
            _manager.SetBusinessDayLength(DayOfWeek.Wednesday, _oldValues[2]);
            _manager.SetBusinessDayLength(DayOfWeek.Thursday, _oldValues[3]);
            _manager.SetBusinessDayLength(DayOfWeek.Friday, _oldValues[4]);
            _manager.SetBusinessDayLength(DayOfWeek.Saturday, _oldValues[5]);
            _manager.SetBusinessDayLength(DayOfWeek.Sunday, _oldValues[6]);
            _isCanceled = true;
            _isExecuted = false;
        }
    }
}
